<!-- Copyright 2010 John L. Reilly

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.   -->

<%@ page import="java.io.IOException"%>
<%@ page import="java.util.Collections"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Map"%>
<%@ page import="java.util.Iterator"%>

<%@ page import="java.util.ListIterator"%>
<%@ page import="java.util.logging.Logger"%>

<%@ page import="javax.cache.Cache"%>
<%@ page import="javax.cache.CacheException"%>
<%@ page import="javax.cache.CacheManager"%>
<%@ page import="javax.servlet.ServletException"%>
<%@ page import="javax.servlet.http.HttpServlet"%>
<%@ page import="javax.servlet.http.HttpServletRequest"%>
<%@ page import="javax.servlet.http.HttpServletResponse"%>

<%@ page import="com.riq.entity.Alert"%>
<%@ page import="com.riq.entity.Member"%>
<%@ page import="com.riq.entity.Department"%>
<%@ page import="com.riq.entity.Location"%>
<%@ page import="com.riq.entity.Tracking"%>
<%@ page import="com.riq.entity.Status"%>

<%@ page import="com.riq.nonpersist.ReportLocation"%>
<%@ page import="com.riq.nonpersist.ReportDept"%>

<%@ page import="com.riq.ServletUtilities"%>


<%

Alert a = (Alert) request.getAttribute("alert");
System.out.println("Alert msg: " + a.getalertMsgString());

String intent = (String) request.getAttribute("intent");
System.out.println("Intent: " + intent);

String path = (String) request.getAttribute("path");
System.out.println("Path: " + path);

List<ReportDept> allDepts = (List<ReportDept>) request.getAttribute("reportDepts");
System.out.println("Depts qty: " + allDepts.size());

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 

<title>FirstResponder</title>

<script type="text/javascript">

function detectScrollbar() {
  if (navigator.appName == "Microsoft Internet Explorer") {
    window.name=document.body.scrollTop;
  } else {
    window.name=window.pageYOffset;
  }
}

function doScroll() {
   if (window.name) window.scrollTo(0, window.name);
}

//toggles the div to hidden
function toggle1(showHideDiv, switchTextDiv) {
  var ele = document.getElementById(showHideDiv);
  var text = document.getElementById(switchTextDiv);
  if (ele.style.display == "block") {
    ele.style.display = "none";
    text.innerHTML = "empty";  
  } else {
    ele.style.display = "block";
    ele.style.display = "none";
    text.innerHTML = "empty";
  }
}


// toggles the div, switches the header text, saves state in cookie
function toggle2(showHideDiv, switchTextDiv) {
  var ele = document.getElementById(showHideDiv);
  var text = document.getElementById(switchTextDiv);
  if (ele.style.display == "block") {
        ele.style.display = "none";
    text.innerHTML = "show";
    setCookie(showHideDiv, 'closed', 365);
  } else {
    ele.style.display = "block";
    text.innerHTML = "hide";
    setCookie(showHideDiv, 'open', 365);
  }
}


// same as toogle2 except takes array (intended to toggle all divs)
function toggle3(contentDiv, controlDiv) {
  if (contentDiv.constructor == Array) {
    for(i=0; i < contentDiv.length; i++) {
         toggle2(contentDiv[i], controlDiv[i]);
    }
  } else {
    toggle2(contentDiv, controlDiv);
  }
}


function getCookie(c_name) {
if (document.cookie.length>0) {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1){
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}


function setCookie(c_name,value,expiredays) {
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}


</script>

<script language="JavaScript" src="/gen_validatorv31.js" type="text/javascript"></script>

<script type="text/javascript">
function validate_required(field,alerttxt) {
	with (field) {
	  if (value==null||value=="") {
	    alert(alerttxt);return false;
	  } else {
	    return true;
	  }
  }
}

function validate_form(thisform) {
	with (thisform) {
	  if (validate_required(locationId,"You must select at least one Location.")==false)
	  {locationId.focus();return false;}
	  if (validate_required(memberId,"You must select at least one Member.")==false)
	  {memberId.focus();return false;}
	  }
}
</script>


<!--META-->
<meta http-equiv="content-type"  content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width" />

<!--STYLESHEET-->
<link rel="stylesheet" type="text/css" href="stylesheets/static.css" />

</head> 
<body onload="doScroll()" onunload="window.name=document.body.scrollTop">


<% 
Cookie[] cookie = request.getCookies(); 
System.out.println("Cookies matching: " + cookie.length);
System.out.println("Inside parCommandMulti");
%>

<!--FIXED HEADERS-->
<div class="headwrap">
<div class="hBlack">
<div class="headerLeftTop">FirstResponder</div>
<div class="headerRightTop">ver 0.9</div>
<div class="headerLeftBottom">Accountability</div>
<div class="headerRightBottom">12:34 PM</div>
</div>
<div class="hGray">
  <a class="buttonGrayHdr" href="mainMenu.html">Main</a>
  <a class="buttonGrayHdr gSelected" href="commandMenu.html">Command</a>
  <a class="buttonGrayHdr" href="department?action=display">Admin</a>
</div>
</div>

<div class="outwrap">
<div class="headerPlugShort"></div>

<% if (a != null) {
System.out.println("Alert not null");
%>
<label for="alertMap">
	<div id="alert">
	<% if (a.getlat() != null && a.getlng() != null) { %>
		<div style="width: 80%; background-color: gold; text-align: center">
		  <div style="padding-left: 10px; padding-right: 10px"><%=a.getalertMsgString() %></div>
	  </div>
		<div style="width: 20%; background-color: goldenrod; text-align: right">
			<div style="padding-right: 10px; float: right" >
			<a id="alertMap" style="float: right" 
			     href="http://maps.google.com/?daddr=<%=a.getlat()%>,<%=a.getlng()%>);" >Map</a> 
			</div>	  
		</div>
	<% } else { %>
	  <div style="width: 100%; background-color: gold">
	  <div style="width: 100%; padding-right: 10px; 
	       padding-left: 10px;"><%=a.getalertMsgString() %></div>
	  </div>
	<% } %>
	</div>
</label>
<% } %>


<!--BUTTONS-->
      
<div id="centeredButtons" style="margin-top: 10px"><ul><li>
<a class="button green narrow tall"
href="reportingPrep?intent=<%=intent%>&alertId=<%=a.getid()%>&path=addResources">
Add<p> Resources</p></a></li>
<li><a class="button green narrow tall"
href="reportingPrep?intent=<%=intent %>&alertId=<%=a.getid()%>">
Refresh<p>Or Reset</p></a></li></ul></div>

<div id="centeredButtons"><ul><li>
<a class="button blue narrow tall last"
href="reportingPrep?intent=activeLocations&alertId=<%=a.getid()%>">
Active<p>Resources</p></a></li>
<li><a class="button blue narrow tall last"
href="reportingPrep?intent=allLocations&alertId=<%=a.getid()%>">
All<p>Resources</p> </a></li></ul></div>

   
  <form id="accountability" onSubmit="return checkform()" onsubmit="return validate_form(this)"
        action="/tracking?action=memberForm&intent=<%=intent %>" method="post">
  <input type=hidden name="alertId" value="<%=a.getid() %>"></input>
  <input type=hidden name="deptId" value="<%=allDepts.get(0).dept().getid()%>"></input>
  <input type=hidden name="intent" value="<%=intent %>"></input>
   
<% 
  String tempLocationType = "init";
  String tempLocationName = "init";
  String tempSelect = "init";

  Long tempLocationId = null;
  Long tempDeptId = null;
  Long currentDept = null;
   
// dept loop
   
for (ReportDept d : allDepts) {
  
   tempLocationType = "init";
   System.out.println("Start Dept Loop: " + d.dept().getshortName());
   System.out.println("Start Dept Size: " + allDepts.size());

   // dept name %>
   <label for="<%=d.dept().getid() %>|dept">
	   <div class="sub gold" style="width: 80%; height: 60px; text-align: left; 
	        border-top: solid black 1px; font-size: 16px; 
	        padding: 12px 10px 8px" ><%=d.dept().getshortName() %>
	   </div>
   </label>
   
   <% 
   System.out.println("In Dept Loop: " + d.dept().getshortName());

   // show & hide department %>
   <a class="sub darkgold" style="clear:none; height: 60px; color: black; 
      border-top: solid black 1px; text-align: right; width: 20%" id="hdrD<%=d.dept().getid()%>"  
      href="javascript:toggle2('conD<%=d.dept().getid() %>', 'hdrD<%=d.dept().getid() %>');" >
      hide</a>
   <div id="conD<%=d.dept().getid() %>" style="display: block;"> 
   
   <% 
   // unassigned %>
   <div class="sub white" style="width: 80%">Unassigned</div> 
   
   <%
   // show & hide: unassigned %>
   <a class="sub blue buttonright" id="hdrUn<%=d.dept().getid() %>" style="clear:none; 
       border: 0px solid black; text-align: right; width: 20%; color: white; text-align: right" 
       href="javascript:toggle2('conUn<%=d.dept().getid() %>', 'hdrUn<%=d.dept().getid() %>');" >
       hide</a>
   <div id="conUn<%=d.dept().getid() %>" style="display: block;"><%

   if (d.unassignedMembers() != null) {
     for (Member unMember : d.unassignedMembers()) {
      System.out.println("Start Unassigned Member Loop: " + unMember.getlastName() + "|" +
      d.unassignedMembers().size()); %>
     <label for="<%=unMember.getid() %>|unassigned">
       <div class="row" style="clear:none; width: 100%">
       <input id="<%=unMember.getid() %>|unassigned" type="checkbox" name="memberId" 
              style="margin-right: 10px; text-align: right" value="<%=unMember.getid() %>"/>
              <%=unMember.getfirstName()%> <%=unMember.getlastName()%>	                  
       </div>
     </label><%
     }
   }
   System.out.println("End Unassigned Member Loop!");
   %>
   </div> <%
   // this section for hiding and showing Unassigned details
   for (int i = 0; i < cookie.length; i++) {
     if (("conUn" + d.dept().getid() ).equalsIgnoreCase(cookie[i].getName())) {
       if ("closed".equals(cookie[i].getValue())) {
         // set div style to invisible and text to "show"
         %>
         <script type="text/javascript" language="JavaScript">
         toggle2('conUn<%=d.dept().getid() %>', 'hdrUn<%=d.dept().getid() %>');
         </script>
         <%
       }
     } 
   }

// process location details
   System.out.println("Start LocationType"); 

  // ctl is sorted by dept asc, locationSeq asc
  for (final String locationType : d.locTypes()) {
  
    System.out.println("In LocationType: " + d.locTypes().size()); 

	  // location type %>
	  <div class="sub black" style="clear: none; text-align: left; 
	       width: 60%"><%=locationType%>
	  </div> <% 
	   
	  // save button %>
	  <div>
	  <a class="sub green buttonright" style="clear:none; text-align: right; width: 20%" 
	  href="#" onclick="javascript:document.forms[0].submit();return false;">Save</a>
	  </div>
	  <% 
	  
	  // show/hide button for div %>
    <a class="sub blue buttonright" style="clear:none; border: 0px solid black; 
        text-align: right; width: 20%; color: white; text-align: right" 
        id="hdr<%=d.dept().getid()%>|<%=locationType%>"  
        href="javascript:toggle2('con<%=d.dept().getid()%>|<%=locationType%>', 'hdr<%=d.dept().getid()%>|<%=locationType%>');" >hide</a>
    <div id="con<%=d.dept().getid()%>|<%=locationType%>" style="display: block;"> 
    
  <% for (final ReportLocation l : d.locations()) {
    if (locationType.equals(l.location().gettype())) {

// location subhead

	   // location name %>
	  <label for="<%=l.location().getid() %>|target">
	    <div class="sub white" style="width: 55%">
	    <label for="<%=l.location().getid() %>|loc"><%=l.location().getshortName()%> (tbd)</label>
	    </div><%
	  
	    // radio button for target location %>
	    <div class="sub white" style="width: 25%; clear: none; text-align: right">
	    <input id="<%=l.location().getid() %>|target" type="radio" name="locationId" 
	           style="text-align: right !important" value="<%=l.location().getid() %>"/>
	    <label for="<%=l.location().getid() %>|target">Dest</label>
	    </div>
	  </label><%
	  
	   // show/hide button for div %>
	  <a class="sub blue buttonright" style="clear:none; border: 0px solid black; 
			 text-align: right; width: 20%; color: white; text-align: right" 
			 id="hdr<%=l.location().getid()%>"  
			 href="javascript:toggle2('con<%=l.location().getid()%>', 'hdr<%=l.location().getid() %>');" >hide</a>
	  <% System.out.println("In Location Subhead: " + l.location().getshortName()); 

// location statuses

   // location content div %>
   <div id="con<%=l.location().getid() %>" style="display: block;"><% 
   if (l.locationStatuses() != null) {
     
    // status drop down %>
     <div class="row palegold" style="width: 100%; color: dimgray">Status: 
     <select name="comboId"  class="button" style="border-color: midnightblue; font-size: 1em">
     <% 
     if (l.selectedStatus() == null) {
       System.out.println("In selected dropdown 1: " + l.location().getshortName());
       %> <option style="color: dimgray" value=pending selected>Pending</option><%
     }
     for (Status s : l.locationStatuses()) {
       System.out.println("In selected dropdown 2: " + l.location().getshortName()); 
       // comboId created from LocationId and StatusId
       String comboId = l.location().getid() + "|" + s.getid();
       if (s.getid().equals(l.selectedStatus())) {
         System.out.println("In selected dropdown 3: " + l.location().getshortName()); %>
         <option style="color: dimgray" value=<%=comboId %> selected >
         <%=s.getshortName() %>
         </option>
     <% } else { 
         System.out.println("In selected dropdown 4: " + l.location().getshortName());  %>
         <option style="color: dimgray" value=<%=comboId %> >
         <%=s.getshortName() %>
         </option>
     <% }
     System.out.println("In selected dropdown 5: " + l.location().getshortName()); 
     }
      %>
     </select>
     </div>
     <%
   }
   
// location members

	   if (l.locMembers() != null) {
		   for (Member m : l.locMembers()) {
		            System.out.println("In locMembers: "); %> 
		      <label for="<%=m.getid() %>|mem">
		      <div class="row" style="clear:none; width: 100%">
		        <input id="<%=m.getid() %>|mem" type="checkbox" name="memberId" 
		               style="margin-right: 10px; margin-top: 5px" value="<%=m.getid() %>"/>
		              <%=m.getfirstName()%>  <%=m.getlastName() %>
		      </div>
		      </label> <%
		    }
	    }
   
		  %> </div> <%  // location content ending div
		 
		  // this section for hiding and showing Location details determined by stored cookie values
		  for (int i = 0; i < cookie.length; i++) {
		    if (("con" + l.location().getid()).equalsIgnoreCase(cookie[i].getName())) {
		      if ("closed".equals(cookie[i].getValue())) {
		        // set div style to invisible and text to "show"  %>
		        <script type="text/javascript" language="JavaScript">
		        toggle2('con<%=l.location().getid() %>','hdr<%=l.location().getid()%>');
		        </script>
		        <%
		      }
		    }
		  }
    } // if loctionType from reportLocations correct
  } // reportLocations loop
    
    %></div><%  // locationtype ending div
    
    // this section for hiding and showing LocationType details determined by stored cookie values
    for (int i = 0; i < cookie.length; i++) {
      if (("con" + d.dept().getid() + "|" + locationType).equalsIgnoreCase(cookie[i].getName())) {
        if ("closed".equals(cookie[i].getValue())) {
          // set div style to invisible and text to "show"  %>
          <script type="text/javascript" language="JavaScript">
          toggle2('con<%=d.dept().getid()%>|<%=locationType %>','hdr<%=d.dept().getid()%>|<%=locationType %>');
          </script>
          <%
        }
      }
    }
  } // locationType loop
  


 %></div><%
   // this section for hiding and showing Department details
  for (int i = 0; i < cookie.length; i++) {
    if (("conD" + d.dept().getid()).equalsIgnoreCase(cookie[i].getName())) {
      if ("closed".equals(cookie[i].getValue())) {
        // set div style to invisible and text to "show"
        %> 
        <script type="text/javascript" language="JavaScript">
        toggle2('conD<%=d.dept().getid()%>','hdrD<%=d.dept().getid()%>');
        </script>
        <%
      }
    }
  }
 
 
} // dept
 
 %>

</form>
 
<div class="hints">Quick Hints:</div>
<div class="reportWrapper">
Step 1. Select one or more Members to be assigned to a new Location.
<br><br>
Step 2. Select one Location (and only one) where the Members will be assigned.
<br><br>
Step 3. Select any green SAVE button to complete the assignment.  
The Accountability Report will refresh and the new assignment will be displayed.
<br><br>
Use the Hide button to hide a Department or Location's details.  They will stay 
hidden until you click the Show button (which replaces the Hide button).
<br><br>
Clicking anywhere in a row will select the row's radio button or checkbox. 
Clicking anywhere on a colored button will select that button.
</div>

<!--FOOTER-->
<div class="footer">FirstResponder</div>

<!--OUTLINE WRAPPER -->
</div> 

</body> 
</html>